package pl.kosmowska.ewelina.client.views.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.OutcomeDataProxy;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.SavingsDataProxy;
import pl.kosmowska.ewelina.client.views.SavingsEditView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;
import pl.kosmowska.ewelina.client.views.widgets.grids.OutcomeDataGrid;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class SavingsEditViewImpl extends Composite implements SavingsEditView{

	private static savingsEditViewImplUiBinder uiBinder = GWT
			.create(savingsEditViewImplUiBinder.class);
	@UiField Button confirmButton;
	@UiField Button clearButton;
	@UiField HorizontalPanel startDatePanel;
	@UiField TextBox nameBox;
	@UiField DoubleBox valueBox;
	@UiField ResizeLayoutPanel installmentPanel;
	@UiField HorizontalPanel finalButtonPanel;


	private OutcomeDataGrid<OutcomeDataProxy> installmentGrid;

	private DateBox startDateBox = new DateBox();
	private DateBox endDateBox = new DateBox();
	
	private Presenter presenter;
	private boolean alreadyInit;
	private SavingsDataProxy savingsData;
	private List<OutcomeDataProxy> outcomeDataList = new  ArrayList<OutcomeDataProxy>();
	private OutcomeDataProxy outcomeDataProxy;
	private String name;
	private float value;
	private Date startDate;
	private Date endDate;


	interface savingsEditViewImplUiBinder extends UiBinder<Widget, SavingsEditViewImpl> {
	}

	public SavingsEditViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		startDatePanel.add(initDateBox(startDateBox, "Oszczędzam od:"));
		startDatePanel.add(initDateBox(endDateBox, "Oszczędzam do:"));
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void reInitView() {
		if(outcomeDataProxy!=null){
			fillFormForOutcomeSavingsPlan();
		}else if(savingsData!=null){
			fillForm();
		}else{
			clearForm();
		}
	}


	private void clearForm() {
		nameBox.setText(null);
		valueBox.setValue(null);
		startDateBox.setValue(null);
		endDateBox.setValue(null);
		nameBox.setEnabled(true);
		valueBox.setEnabled(true);
		endDateBox.setEnabled(true);
		finalButtonPanel.setVisible(false);
		installmentPanel.setVisible(false);
	}

	private void fillFormForOutcomeSavingsPlan() {
		nameBox.setText(outcomeDataProxy.getName());
		valueBox.setValue((double)abs(outcomeDataProxy.getValue()));
		startDateBox.setValue(new Date());
		endDateBox.setValue(outcomeDataProxy.getDate());
		nameBox.setEnabled(false);
		valueBox.setEnabled(false);
		endDateBox.setEnabled(false);
	}

	public void setData(SavingsDataProxy savingsData) {
		this.savingsData = savingsData;
	}
	private float abs(float value) {
		if (value>0)
			return value;
		else
			return (-value);
	}
	private void fillForm() {
		nameBox.setText(savingsData.getName());
		valueBox.setValue((double)abs(savingsData.getValue()));
		startDateBox.setValue(savingsData.getStartDate());
		endDateBox.setValue(savingsData.getEndDate());

	}

	public HorizontalPanel initDateBox(DateBox dateBox, String labelName){
		// Create a DateBox
		DateTimeFormat dateFormat = DateTimeFormat.getLongDateFormat();
		dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));

		// Combine the widgets into a panel and return them
		HorizontalPanel vPanel = new HorizontalPanel();
		vPanel.add(new HTML(labelName));
		vPanel.add(dateBox);
		return vPanel;
	}


	@UiHandler("clearButton")
	void onClearButtonClick(ClickEvent event) {
		clearForm();
	}
	@UiHandler("returnButton")
	void onReturnButtonClick(ClickEvent event) {
		presenter.goToSavingsPlace();
	}
	@UiHandler("calculateButton")
	void onCalculateButtonClick(ClickEvent event) {
		if(validateForm()){
			name =nameBox.getText();
			value = valueBox.getValue().floatValue();
			startDate =startDateBox.getValue();
			endDate =endDateBox.getValue();
			
			presenter.generateOutcomeList(name, value, startDate, endDate);
			
		}else{
			showMessage(ViewMessages.ERROR_WRONG_INPUT);
		}

	}


	private boolean validateForm() {
		if(validate(nameBox)&&validate(valueBox)&&validate(startDateBox)&&validate(endDateBox)&&validate(startDateBox.getValue(), endDateBox.getValue())){
			return true;
		}else{
			return false;
		}
	}

	private boolean validate(Date startDate, Date endDate) {
		if(startDate.getTime()>endDate.getTime())
			return false;
		else
			return true;
	}
	private boolean validate(DateBox dateBox) {
		if(dateBox.getValue()==null)
			return false;
		else
			return true;
	}
	
	private boolean validate(DoubleBox doubleBox) {
		if(doubleBox.getValue()==null)
			return false;
		else
			return true;
	}
	
	private boolean validate(TextBox textbox) {
		if(textbox.getText()==null || textbox.getText().equals("") )
			return false;
		else
			return true;
	}
	@Override
	public void showGrid(){
		finalButtonPanel.setVisible(true);
		installmentPanel.clear();
		installmentPanel.setVisible(true);
		installmentGrid = new OutcomeDataGrid<OutcomeDataProxy>();
		installmentGrid.setHeight("600px");
		installmentGrid.setDataList(outcomeDataList);			
		installmentPanel.add(installmentGrid);
	}


	@UiHandler("confirmButton")
	void onConfirmButtonClick(ClickEvent event) {	    
		presenter.saveSavingsData(name, value,startDate,endDate,outcomeDataList);
	}

	@Override
	public void setOutcomeProxy(OutcomeDataProxy outcomeDataProxy) {
		this.outcomeDataProxy=outcomeDataProxy;

	}
	
	@Override
	public void showMessage(String errorMessage) {
		MessageDialogBox messageDialogBox = new MessageDialogBox(errorMessage);
		messageDialogBox.center();
		messageDialogBox.show();

	}
	
	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}
	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}

	@Override
	public void setOutcomeDataList(List<OutcomeDataProxy> outcomeDataList) {
		this.outcomeDataList = outcomeDataList;
	}
	

}
